package com.imranstechnology.homeexpensemanager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.imranstechnology.homeexpensemanager.utils.DateUtil;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;

public class MonthlyExpenseView extends Activity {

	private List<Map<String,Float>> details;
	private LayoutInflater inflater;
	private List<ExpenseDetailEntity> expenseTransactions;
	private ListView listViewRecords;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_monthly_expense_view);
		inflater = getLayoutInflater();
		
		expenseTransactions = new ArrayList<ExpenseDetailEntity>();
		for(Integer year : ExpenseMemStore.EXPENSEE_ENTITY.getYearsTrnx().keySet())
		{
			expenseTransactions.addAll(ExpenseMemStore.EXPENSEE_ENTITY.getYearRecords(year));
		}
				
		Collections.sort(expenseTransactions, new Comparator<ExpenseDetailEntity>() {
			@Override
			public int compare(ExpenseDetailEntity lhs, ExpenseDetailEntity rhs) {
				if(rhs.getTransactionDate() == null ||lhs.getTransactionDate() == null)
					return 0;
				return rhs.getTransactionDate().compareTo(lhs.getTransactionDate());
			}
		});
		details = new ArrayList<Map<String,Float>>();
		Date lastDate = null;
		Map<String,Float> rec = null;

		for(ExpenseDetailEntity ede:expenseTransactions){
			Date trnxDate = ede.getTransactionDate();
			String key = ""+DateUtil.getDateFiled(trnxDate, Calendar.YEAR)+"/"+(DateUtil.getDateFiled(trnxDate, Calendar.MONTH)+1);
			if(!DateUtil.isSameMonth(lastDate, trnxDate)){
				rec = new HashMap<String, Float>();
				rec.put(key, 0f);
				details.add(rec);
			}
			Float value = rec.get(key);
			value += ede.getExpenseAmount();
			rec.put(key, value);
			lastDate = trnxDate;
		}
		listViewRecords = (ListView) findViewById(R.id.dynamic_monthly_list_view);
		BaseAdapter adapter = new MonthlyExpenseViewAdapter();
		listViewRecords.setAdapter(adapter);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.monthly_expense_view, menu);
		return true;
	}
	private class MonthlyExpenseViewAdapter extends BaseAdapter{
		private TableRow renderedRow;
		
		public MonthlyExpenseViewAdapter() {
			super();
		}
		@Override
		public int getCount() {
			if(details == null)
				return 0;
			return details.size();
		}
		@Override
		public Object getItem(int position) {
			if(details == null)
				return null;
			return details.get(position);
		}
		@Override
		public long getItemId(int position) {
			if(details == null)
				return 0;
			//return ((ExpenseDetailEntity)details.get(position)).getId();
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View tableRow = convertView;
			if(tableRow == null)
			{
				tableRow = inflater.inflate(R.layout.monthly_list_view_table_row, null);
			}	
			
			Map<String,Float> rec = details.get(position);
			String key = rec.keySet().iterator().next();
			Float value = rec.get(key);
			TextView view = (TextView)tableRow.findViewById(R.id.month_column_table_row);
			view.setText(""+key);
			view = (TextView)tableRow.findViewById(R.id.monthly_amount_column_table_row);
			view.setText(""+value);
			
			return tableRow;
		}
	};
}
